<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Block extends Admin_Controller
{	
	function dropdown()
	{
		global $CI;
		
		// model
		$CI->load->module_model('block.block_dropdown_model');
		
		// add to view
		$this->add('name', $CI->block_dropdown_model->name);
		$this->add('onchange', $CI->block_dropdown_model->onchange);
		$this->add('value', $CI->block_dropdown_model->value);
		$this->add('items', $CI->block_dropdown_model->items);
		
		// view
		$this->view('block_dropdown_view');
	}
	
	function lists()
	{
		global $CI;
		
		// model
		$CI->load->module_model('block.block_list_model');
		$CI->load->module_model('image.display_image_model');
		
		// filter
		$CI->load->module_model('block_type.block_type_dropdown_model');
		$CI->block_type_dropdown_model->name = 'filter';
		$CI->block_type_dropdown_model->onchange = ' onchange="block.filter()"';
		if (isset($_POST['filter']))
			$CI->block_list_model->cache->add($CI->block_list_model->filter_cache_prefix, $_POST['filter']);
		$ts = $CI->block_list_model->cache->load($CI->block_list_model->filter_cache_prefix);
		$CI->block_type_dropdown_model->value = $ts;

		// sql
		$CI->db->where(BLOCK.'.'.BLOCK_BLOCK_TYPE, $ts);
		$CI->db->where(BLOCK.'.'.BLOCK_FRANCHISE_ID, $CI->block_list_model->franchise_id());
		$q = $CI->db->get(BLOCK);
		
		// pagenavigation
		$CI->load->library('pagination');
        $config['base_url'] = site_url($CI->block_list_model->url_prefix.'/%s');
        $config['total_rows'] = $q->num_rows();
        $config['per_page'] = $CI->config->item('rows_per_page');
		$config['cur_page'] = $CI->block_list_model->start;
		$config['num_links'] = $CI->config->item('num_links');		
        $CI->pagination->initialize($config);
        $this->add('paging', $CI->pagination->create_links());
		
		// sql
		$CI->db->from(BLOCK);
		$CI->db->select(BLOCK_BLOCK_ID.','.BLOCK_NAME.','.BLOCK_CONTENT.','.BLOCK_OBJ_ID);
		$CI->db->where(BLOCK.'.'.BLOCK_FRANCHISE_ID, $CI->block_list_model->franchise_id());
		$CI->db->where(BLOCK.'.'.BLOCK_BLOCK_TYPE, $ts);
		//$CI->db->join(IMAGE, IMAGE.'.'.IMAGE_IMAGE_ID.'='.BLOCK.'.'.BLOCK_OBJ_ID, 'left');
		//$CI->db->where(IMAGE.'.'.IMAGE_FRANCHISE_ID, $CI->block_list_model->franchise_id());
		foreach ($CI->block_list_model->columns as $k=>$v)
		{
			if ($CI->block_list_model->cache->load($k))
				$CI->db->order_by($v, $CI->block_list_model->cache->load($k));
		}
		$CI->db->limit($CI->config->item('rows_per_page'), $CI->block_list_model->start);
		$q = $CI->db->get();
		
		// gridview
		$this->add('items', $q->result());
		$this->add('start', $CI->block_list_model->start);
		foreach ($CI->block_list_model->columns as $k=>$v)
		{
			if ($CI->block_list_model->cache->load($k))
				$this->add('sort_'.$k,  'class="sort_column_'.$CI->block_list_model->cache->load($k).'"');
			else
				$this->add('sort_'.$k,  '');
		}

		// message
		$this->add('msg', $CI->block_list_model->cache->load($CI->block_list_model->msg_cache_prefix));
		$CI->block_list_model->cache->add($CI->block_list_model->msg_cache_prefix, '');
		
		// toolbar
		$this->module_toolbar('block.toolbar', array('parent_id'=>0, 'start'=>$CI->block_list_model->start));
		$this->module_navigation('Block Management');
		
		// view
		$this->view('block_list_view');
	}
	
	function detail()
	{
		global $CI;
		
		// model
		$CI->load->module_model('block.block_detail_model');
		
		// sql	
		$q = $CI->db->get_where(BLOCK, array(BLOCK_FRANCHISE_ID=>$CI->block_detail_model->franchise_id(), BLOCK_BLOCK_ID=>$CI->block_detail_model->item));
		$entity = $q->row();
		
		// error
		$post = $CI->block_detail_model->cache->load('post');
		$CI->block_detail_model->cache->add('post', '');

		// add to view
		$this->add('name', $post?$post['name']:($entity?$entity->{BLOCK_NAME}:''));
		$block_type = $post?$post['block_type']:($entity?$entity->{BLOCK_BLOCK_TYPE}:$CI->block_detail_model->cache->load('filter'));
		if (!$block_type)
			// go to original page
			redirect($CI->block_detail_model->url_prefix2.'/'.$CI->block_detail_model->start);
	
		$this->add('block_type', $block_type);
		
		// editor
		$CI->load->library('fckeditor');
		$CI->fckeditor->InstanceName = 'content';
		$CI->fckeditor->Value = $post?$post['content']:($entity?$entity->{BLOCK_CONTENT}:'');
		$CI->fckeditor->SetPathOfFranchise();
		$this->add('content', $CI->fckeditor->CreateHtml());
		
		// image dropdown
		$CI->load->module_model('image.image_dropdown_model');
		$CI->image_dropdown_model->name = 'obj_id';
		$CI->image_dropdown_model->value = $post?$post['obj_id']:($entity?$entity->{BLOCK_OBJ_ID}:'');
		$CI->image_dropdown_model->onchange = ' onchange="display_image(this)"';
		$CI->image_dropdown_model->addOption(array(IMAGE_IMAGE_ID=>0, IMAGE_NAME=>'----- Select Image -----'));
		
		// image review
		$CI->load->module_model('image.display_image_model');
		$this->add('image_review', $CI->display_image_model->thumbnail($CI->image_dropdown_model->value));
		
		// add to view
		$this->add('start', $CI->block_detail_model->start);
		$this->add('item_id', $CI->block_detail_model->item);

		// message
		$this->add('msg', $CI->block_detail_model->cache->load($CI->block_detail_model->msg_cache_prefix));
		$CI->block_detail_model->cache->add($CI->block_detail_model->msg_cache_prefix, '');
		
		// toolbar
		$this->module_toolbar();
		if ($CI->block_detail_model->item)
			$this->module_navigation('Block : Edit');
		else
			$this->module_navigation('Block : New');
			
		// view
		$this->view('block_detail_view');
	}
}

?>